<template>
  <div class="home-container">
    <!-- 导航栏 -->
    <van-nav-bar class="page-nav-bar" title="登录" fixed>
      <van-button slot="title" type="info" class="seach-btn"
      size="small" round icon="search" @click="$router.push('/search')">搜索</van-button>
    </van-nav-bar>

    <!-- 频道列表 -->
    <!-- 通过 v-model 绑定当前激活标签对应的索引值，默认情况下启用第一个标签 -->
    <!-- - 通过 animated 属性可以开启切换标签内容时的转场动画 -->
    <!-- - 通过 swipeable 属性可以开启滑动切换标签页 -->
    <van-tabs class="channel-tabs" v-model="active" swipeable animated>
      <van-tab :title="channel.name"
      v-for="channel in channels"
      :key="channel.id">
        <!-- 文章列表 组件 -->
        <article-list ref="article-list" :channel="channel" />
      </van-tab>

      <!-- 占位元素,解决最后一个tab被遮挡问题 -->
      <div slot="nav-right" class="placeholder"></div>
      <!-- 汉堡按钮 -->
      <div slot="nav-right" class="hamburger-btn"
      @click="isChannelEditShow = true">
        <i class="iconfont icongengduo"></i>
      </div>
    </van-tabs>

    <!-- 频道编辑弹出层 -->
    <van-popup
    v-model="isChannelEditShow"
    closeable
    close-icon-position="top-left"
    position="bottom"
    :style="{ height: '100%' }">
      <channel-edit :user-channels="channels"
      :active-index="active"
      @update-active="onUpdateActive" />
    </van-popup>
  </div>
</template>

<script>
// 获取 json 数据
import channelsList from '@/json/home/channels.json'
// 文章列表
import ArticleList from './components/article-list'
// 弹出层
import ChannelEdit from './components/channel-edit'
// vuex
import { mapState } from 'vuex'
// 本地存储模块
import { getItem } from '@/utils/storage.js'
export default {
  name: 'HomeIndex',
  components: {
    ArticleList,
    ChannelEdit
  },
  props: {},
  data () {
    return {
      active: 0,
      // 频道数据列表
      channels: [],
      // 控制编辑频道弹出层的显示状态
      isChannelEditShow: false
    }
  },
  computed: {
    ...mapState(['user'])
  },
  watch: {},
  created () {
    // 渲染频道数据
    this.loadChannels()
  },
  mounted () {},
  methods: {
    // 获取频道数据
    async loadChannels () {
      const localChannels = getItem('TOUTIAO_CHANNELS')
      // 判断是否存在本地的频道列表数据   没有，请求获取默认频道列表
      this.channels = localChannels || channelsList
    },
    // 接收父组件传递数据，实现频道切换
    onUpdateActive (index, isChannelEditShow = true) {
      this.active = index
      this.isChannelEditShow = isChannelEditShow
    }
  }
}
</script>

<style scoped lang="less">
.home-container{
  padding-bottom: 100px;
  padding-top: 174px;
  /deep/.van-nav-bar__title{
    max-width: unset;
  }
  // 搜索框
  .seach-btn{
    width: 555px;
    height: 64px;
    background-color: #5babfb;
    border: none;
    font-size: 28px;
    .van-icon {
      font-size: 32px;
    }
  }

  // tab切换栏
  /deep/ .channel-tabs {
    .van-tabs__wrap {
      position: fixed;
      top: 92px;
      z-index: 1;
      left: 0;
      right: 0;
      height: 82px;
    }
    .van-tab {
      min-width: 200px;
      border-right: 1px solid #edeff3;
      font-size: 30px;
      color: #777777;
    }
    .van-tab--active {
      color: #333333;
    }
    .van-tabs__nav {
      padding-bottom: 0;
    }
    .van-tabs__line {
      bottom: 8px;
      width: 31px !important;
      height: 6px;
      background-color: #3296fa;
    }

    // 占位元素
    .placeholder {
      // flex-shrink默认是1,参与剩余空间的计算flex-shrink: 0;不参与
      flex-shrink: 0;
      width: 66px;
      height: 82px;
    }

    // 汉堡按钮
    .hamburger-btn {
      position: fixed;
      right: 0;
      display: flex;
      justify-content: center;
      align-items: center;
      width: 66px;
      height: 82px;
      background-color: #fff;
      opacity: .9;
      // 图标
      i.iconfont {
        font-size: 33px;
      }
      // 左边框
      &:before {
        content: '';
        position: absolute;
        left: 0;
        width: 1px;
        height: 100%;
        background-image: url(~@/assets/gradient-gray-line.png);
        background-size: contain;
      }
    }
  }
}
</style>
